/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package BoardGame.impl;

import BoardGame.Board;
import BoardGame.BoardGamePackage;
import BoardGame.CodeAdapter;
import BoardGame.Field;
import BoardGame.Figure;

import MocaTree.File;
import MocaTree.MocaTreeFactory;
import MocaTree.Node;

import de.upb.tools.sdm.*;

import java.lang.reflect.InvocationTargetException;

import java.util.*;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;

import org.eclipse.emf.ecore.impl.EObjectImpl;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Code Adapter</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * </p>
 *
 * @generated
 */
public class CodeAdapterImpl extends EObjectImpl implements CodeAdapter {
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected CodeAdapterImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return BoardGamePackage.Literals.CODE_ADAPTER;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public File transformModelToText(Board board) {
		boolean fujaba__Success = false;
		File file = null;
		Node boardNode = null;
		Iterator fujaba__IterBoardNodeToRowNode = null;
		Object _TmpObject = null;
		Node rowNode = null;
		Node columnNode = null;
		Figure figure = null;
		Node figureNode = null;
		Iterator fujaba__IterBoardToField = null;
		Field field = null;

		// story pattern build basic tree structure
		try {
			fujaba__Success = false;

			// create object file
			file = MocaTreeFactory.eINSTANCE.createFile();
			// create object boardNode
			boardNode = MocaTreeFactory.eINSTANCE.createNode();
			// assign attribute boardNode
			boardNode.setName("BOARD");
			// create link
			boardNode.setFile(file);

			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		// story pattern itterate over all fields contained in the board game
		try {
			fujaba__Success = false;

			// check object board is really bound
			JavaSDM.ensure(board != null);
			// iterate to-many link fields from board to field
			fujaba__Success = false;

			fujaba__IterBoardToField = new ArrayList(board.getFields())
					.iterator();

			while (fujaba__IterBoardToField.hasNext()) {
				try {
					field = (Field) fujaba__IterBoardToField.next();

					// check object field is really bound
					JavaSDM.ensure(field != null);
					// story pattern If a corresponding row does not exist for the field, create the row and with the new cloumn
					try {
						fujaba__Success = false;

						// check object boardNode is really bound
						JavaSDM.ensure(boardNode != null);
						// iterate to-many link children from boardNode to rowNode
						fujaba__Success = false;

						fujaba__IterBoardNodeToRowNode = boardNode
								.getChildren().iterator();

						while (!(fujaba__Success)
								&& fujaba__IterBoardNodeToRowNode.hasNext()) {
							try {
								_TmpObject = fujaba__IterBoardNodeToRowNode
										.next();

								// ensure correct type and really bound of object rowNode
								JavaSDM.ensure(_TmpObject instanceof Node);
								rowNode = (Node) _TmpObject;

								// check optional
								if (rowNode != null) {
									// check isomorphic binding between objects rowNode and boardNode
									JavaSDM.ensure(!rowNode.equals(boardNode));

									// attribute condition
									JavaSDM.ensure(rowNode.getIndex() == field
											.getY());

								}
								fujaba__Success = true;
							} catch (JavaSDMException fujaba__InternalException) {
								fujaba__Success = false;
							}
						}
						if (!fujaba__Success) {
							fujaba__Success = true;
							rowNode = null;
						}
						// create object rowNode
						if (rowNode == null) {
							rowNode = MocaTreeFactory.eINSTANCE.createNode();
						}

						// create object columnNode
						columnNode = MocaTreeFactory.eINSTANCE.createNode();
						// check optional
						if (rowNode != null) {
							// assign attribute rowNode
							rowNode.setIndex(field.getY());
							// assign attribute rowNode
							rowNode.setName("ROW");
						}// assign attribute columnNode
						columnNode.setIndex(field.getX());
						// assign attribute columnNode
						columnNode.setName("COLUMN");
						// assign attribute boardNode
						boardNode.setName("BOARD");
						// check optional
						if (rowNode != null) {
							// create link
							columnNode.setParentNode(rowNode);

							// create link
							boardNode.getChildren().add(rowNode);

						}
						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}

					// story pattern If a field has a figure add a figure node to the tree
					try {
						fujaba__Success = false;

						// check object columnNode is really bound
						JavaSDM.ensure(columnNode != null);
						// check object field is really bound
						JavaSDM.ensure(field != null);
						// bind object
						figure = field.getFigure();

						// check object figure is really bound
						JavaSDM.ensure(figure != null);

						// create object figureNode
						figureNode = MocaTreeFactory.eINSTANCE.createNode();
						// assign attribute figureNode
						figureNode.setName(figure.eClass().getName());
						// create link
						figureNode.setParentNode(columnNode);

						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}

					fujaba__Success = true;
				} catch (JavaSDMException fujaba__InternalException) {
					fujaba__Success = false;
				}
			}
			JavaSDM.ensure(fujaba__Success);
			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		return file;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eInvoke(int operationID, EList<?> arguments)
			throws InvocationTargetException {
		switch (operationID) {
		case BoardGamePackage.CODE_ADAPTER___TRANSFORM_MODEL_TO_TEXT__BOARD:
			return transformModelToText((Board) arguments.get(0));
		}
		return super.eInvoke(operationID, arguments);
	}

} //CodeAdapterImpl
